﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace _1241108_1241150
{
    public partial class MainForm : Form
    {
        private bool flag = false;

        public MainForm()
        {
            InitializeComponent();
        }
        #region Khai báo biến.
        MyFunction myFunc = new MyFunction();
        

        #endregion



        //Mở file
        private void btnChonFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();

            open.InitialDirectory = "C:\\";
            open.Filter = "txt files (*.txt)|*.txt";
            open.FilterIndex = 1;
            open.RestoreDirectory = true;

            if (open.ShowDialog() == DialogResult.OK)
            {
                //try
                //{
                    string fileName = open.FileName;

                    myFunc.ReadFile(fileName);
                    
                    this.LoadDSThuocTinh();
                    lbCount.Text = myFunc.TapMauHuanLuyen.Rows.Count.ToString();
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show("Không thể đọc File này. Lỗi: " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //}
            }
        }

        private void LoadDSThuocTinh()
        {
            lvThuocTinh.DataSource = myFunc.LoadTenCot();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //danh sách mẫu huấn luyện.
            ShowData showDT = new ShowData(myFunc.TapMauHuanLuyen);

            showDT.ShowDialog();
        }

        private void btnShowMauThuNghiem_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                myFunc.KNNAlg();
                ShowData showDT = new ShowData(myFunc.TapHuyenLuyenAfter);

                showDT.ShowDialog();
            }
            else
            {
                ShowData showDT = new ShowData(myFunc.TapThuNghiem);

                showDT.ShowDialog();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();

            save.Filter = "Text File|*.txt";
            save.FileName = "Output";

            if (save.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(save.FileName);

                DataTable w = myFunc.TapHuyenLuyenAfter;

                for (int i = 0; i < w.Rows.Count; i++)
                {
                    writer.WriteLine(w.Rows[i][w.Columns.Count-1]);
                }


                writer.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            flag = true;
        }
    }
}
